package config

import (
	"dingbot/errhelp"
	"dingbot/global"
	"dingbot/tools"
	"io/ioutil"
	"os"

	"github.com/dingdinglz/dingtools/dingjson"
	"github.com/dingdinglz/dingtools/dinglog"
)

var config_data ConfigModal

func Load() {
	global.Logger.Info("正在载入config.json...")
	if !tools.IsFileExist("config.json") {
		global.Logger.Error("config.json Not Found!")
		os.Exit(1)
	}
	config_data_b, err := ioutil.ReadFile("config.json")
	errhelp.Abort(err, "read json")
	j := dingjson.NewFromBytes(config_data_b)
	err = j.ConventToStruct(&config_data)
	errhelp.Abort(err, "json paser")
	if config_data.Debug {
		global.Logger.SetLevel(dinglog.Level_Debug)
		global.Logger.Debug(config_data)
	}
}
